// 	ID : pallab81
// 	PROG : 10534
// 	LANG : C++
// 	2011-08-26-17.34.16 Friday
//
// 	"I have not failed, I have just found 10000 ways that won't work.
//


#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>

using namespace std;

#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define fo(i,ed) for(int i = 0 ; i < ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

inline void wait( double seconds ){
    double endtime = clock() + ( seconds*CLOCKS_PER_SEC );
    while( clock() < endtime ){;}
}
const int big = (int)1e4;
int n;
vector< int > w,rw;

inline void Read(){
    w.assign(n,0);
    rw.assign(n,0);
    fo(i,n){
        CI( w[i] );
        rw[ n-(i+1) ] = w[i];
    }
}
inline vector<int> find_lis(vector<int> &a){
   vector< int > lis( SZ(a),0 );
   vector< int > b(SZ(a));
   int maxLen = 1, lowBound;
   lis[0]=1,b[0]=a[0];

   for(int i = 1; i < SZ(a); ++i ){
       lowBound = lower_bound(b.begin(),b.begin()+maxLen, a[i] )-b.begin();
       lis[i]=lowBound+1;
       if( lowBound == maxLen ){
           b[ maxLen ] = a[i];
           ++maxLen;
       }
       else{
           b[ lowBound ] = a[i];
       }
   }
return lis;
}
inline void Proc(){
    vector<int> lisF = find_lis(w);
    vector<int> lisB = find_lis(rw);

    int mx=-1;
    fo(i,n){
        mx = max(mx , min(lisF[i],lisB[n-(i+1)]) );
    }
    printf("%d\n", (mx*2) -1 );
}

int main(){
    #ifndef ONLINE_JUDGE
        freopen("in.txt","rt",stdin);
    #endif

    while( scanf("%d",&n)==1 ){
        Read();
        Proc();
    }
return 0;
}
